<?php 
//注册插件
RegisterPlugin("zbgl", "zbgl_ActivePlugin");

//接口
function zbgl_ActivePlugin() {
	Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu', 'zbgl_AddMenu');
	Add_Filter_Plugin('Filter_Plugin_ViewPost_Template','zbgl_tpdx');
	Add_Filter_Plugin('Filter_Plugin_Zbp_MakeTemplatetags','zbgl_highslide');
}

//主题配置
function zbgl_AddMenu(&$m){
	global $zbp;
	array_unshift($m, MakeTopMenu("root",'主题配置',$zbp->host . "zb_users/theme/zbgl/main.php?act=ztsm","","topmenu_zbgl"));
}

//配置名称
function zbgl_SubMenu($id){
	$arySubMenu = array(
		0 => array('模板说明', 'ztsm', 'left', false),
		1 => array('基本设置', 'jbpz', 'left', false),
	);
	foreach($arySubMenu as $k => $v){
		echo '<a href="?act='.$v[1].'" '.($v[3]==true?'target="_blank"':'').'><span class="m-'.$v[2].' '.($id==$v[1]?'m-now':'').'">'.$v[0].'</span></a>';
	}
}

//个性化时间
function zbgl_TimeAgo( $ptime ) {
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if($etime < 1) return '刚刚';
    $interval = array (
        12 * 30 * 24 * 60 * 60  =>  '年前 ('.date('Y/m/d', $ptime).')',
        30 * 24 * 60 * 60       =>  '个月前 ('.date('m/d', $ptime).')',
        7 * 24 * 60 * 60        =>  '周前 ('.date('m/d', $ptime).')',
        24 * 60 * 60            =>  '天前',
        60 * 60                 =>  '小时前',
        60                      =>  '分钟前',
        1                       =>  '秒前'
    );
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . $str;
        }
    };
}

/*最新文章*/
function zbgl_PostTime(){
    global $zbp;
    $str = '';
$order = array('log_PostTime'=>'DESC');
$where = array(array('=','log_Status','0'));
$array = $zbp->GetArticleList(array('*'),$where,$order,array(8),'');
	foreach ($array as $p=>$hotlist)
	{$k = $p+1;
	$str .= '<li><span class="key-'.$k.'">'.$k.'</span><a href="'.$hotlist->Url.'" title="'.$hotlist->Title.'">'.$hotlist->Title.'</a></li>';
	}
	return $str;
}

/*最新评论*/
function zbgl_comment(){
    global $zbp;
    $str = '';
	$order = $zbp->modulesbyfilename['comments']->MaxLi;
	if ($order == 0) $order = 8;
	$comments = $zbp->GetCommentList('*', array(array('=', 'comm_IsChecking', 0)), array('comm_PostTime' => 'DESC'), $order, null);
	foreach ($comments as $comment){
	$str .= '<li><img src="'.$comment->Author->Avatar.'"><a href="' . $comment->Post->Url .'#cmt' . $comment->ID .'"><span><strong>'.$comment->Author->StaticName.'</strong></span>' . SubStrUTF8(TransferHTML($comment->Content,'[nohtml]'),20) . '</a></li>';
	}
	return $str;
}

/*评论最多文章*/
function zbgl_CommNums(){
    global $zbp;
    $str = '';
$order = array('log_CommNums'=>'DESC');
$where = array(array('=','log_Status','0'));
$array = $zbp->GetArticleList(array('*'),$where,$order,array(8),'');
	foreach ($array as $p=>$Commlist)
	{$k = $p+1;
	$str .= '<li><span class="key-'.$k.'">'.$k.'</span><a href="'.$Commlist->Url.'" title="'.$Commlist->Title.'">'.$Commlist->Title.'</a></li>';
	}
	return $str;
}

/*访问最多文章*/
function zbgl_ViewNums(){
    global $zbp;
    $str = '';
$order = array('log_ViewNums'=>'DESC');
$where = array(array('=','log_Status','0'));
$array = $zbp->GetArticleList(array('*'),$where,$order,array(8),'');
	foreach ($array as $p=>$hotlist)
	{$k = $p+1;
	$str .= '<li><span class="key-'.$k.'">'.$k.'</span><a href="'.$hotlist->Url.'" title="'.$hotlist->Title.'">'.$hotlist->Title.'</a></li>';
	}
	return $str;
}

/*推荐图片*/
function zbgl_Istop(){
	 global $zbp;
	 $str = '';
$array = GetList(4, null, null, null, null, null, array("only_ontop"  => true));
foreach ($array as $Istop){
$temp=mt_rand(1,8);
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$content = $Istop->Content;
preg_match_all($pattern,$content,$matchContent);
if(isset($matchContent[1][0])) 
$temp=$matchContent[1][0];
else
$temp=$zbp->host."zb_users/theme/zbgl/style/images/random/$temp.jpg";
$str .= '<li><a href="'.$Istop->Url.'"><img src="'.$temp.'"/><span>'.$Istop->Title.'</span></a></li>';
}
return $str;
}

/*图片延迟加载*/
function zbgl_tpdx(&$template){
global $zbp;
if($zbp->Config('zbgl')->tpdx){
$article = $template->GetTags('article');
$pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>/i";
$str = '<a href=$2$3.$4$5 onclick="return hs.expand(this)"><img src=$2$3.$4$5 alt="'.$article->Title.'"/></a>';
$content = preg_replace($pattern , $str, $article->Content);
$article->Content = $content;
$template->SetTags('article', $article);
}
if($zbp->Config('zbgl')->tpyc){
$article = $template->GetTags('article');
$pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>/i";
$str = '<img class="b-lazy" src="'.$zbp->host.'zb_users/theme/zbgl/style/images/1.gif" data-src=$2$3.$4$5 alt="'.$article->Title.'" >';
$content = preg_replace($pattern, $str, $article->Content);
$article->Content = $content;
$template->SetTags('article', $article);
}
}

/*图片灯箱效果*/
function zbgl_highslide(){
global $zbp;
if($zbp->Config('zbgl')->tpdx){
$str='<script type="text/javascript" src="'.$zbp->host.'zb_users/theme/zbgl/script/highslide-with-gallery.js"></script> 
	<script type="text/javascript">
	hs.graphicsDir =\''.$zbp->host.'zb_users/theme/zbgl/style/css/graphics/\';
	hs.align = \'center\';
	hs.transitions = [\'expand\', \'crossfade\'];
	hs.fadeInOut = true;
	hs.dimmingOpacity = 0.8;
	hs.outlineType = \'rounded-white\';
	hs.captionEval = \'this.thumb.alt\';
	hs.marginBottom = 105; // make room for the thumbstrip and the controls
	hs.numberPosition = \'caption\';

	// Add the slideshow providing the controlbar and the thumbstrip
	hs.addSlideshow({
		//slideshowGroup: \'group1\',
		interval: 5000,
		repeat: false,
		useControls: true,
		overlayOptions: {
			className: \'text-controls\',
			position: \'bottom center\',
			relativeTo: \'viewport\',
			offsetY: -60
		},
		thumbstrip: {
			position: \'bottom center\',
			mode: \'horizontal\',
			relativeTo: \'viewport\'
		}
	});
</script> ';
$zbp->footer .= $str. "\r\n";
$zbp->header .='<link rel="stylesheet" type="text/css" href="'.$zbp->host.'zb_users/theme/zbgl/style/css/highslide.css"/>' . "\r\n";
}
}

?>